import Dict from '@/api/dict.js'
export default {
  namespaced: true,
  state: () => {
    return {
      organizations: [],
      clientTypes: [],
      clientTiers: [],
      countriesOrRegions: [],
      timeZones: [],
      businessAreas: [],
      salesBUs: [],
      myBUs: [],
      operateBUs: [],
      ownersBUs: [],
      allHrSalesBUs: [],
      genders: [
        {
          value: 0,
          text: 'Unknown'
        },
        {
          value: 1,
          text: 'Male'
        },
        {
          value: 2,
          text: 'Female'
        }
      ],
      contactTypes: [],
      invoiceFileTypes: [],
      IMTypes: [],
      communicationMethods: [],
      communicationCategories: [],
      currencies: [],
      clientOperateTypes: [],
      clientStatusTypes: [],
      /* 报价单 */
      // 竟对信息
      rivals: [],
      // 竞对定位
      rivalLevels: [],
      // 客户价值
      clientValues: [],
      // 需求类型
      demandTypes: [],
      // 预测PO
      forecastPOs: [],
      // 内部本地化流程
      internalLocalizationProcess: [],
      // 采购流程
      procurementProcess: [],
      // 目前痛点
      painPoints: [],
      // 沟通渠道
      communicationChannels: [],
      // 沟通方式
      communicationModes: [],
      reviewInfos: [],
      // 平台要求
      platforms: [],
      // 译文用途
      translationPurpose: [],
      // 报价策略
      quotationStrategies: [],
      // 客户分公司
      hrOrg: [],
      // 合同类型
      clientContractTypes: [],
      // eci公司
      eciCompanies: [],
      // 付款方式
      paymentMethods: [],
      // 冻结原因
      freezeReasons: []
    }
  },
  mutations: {
    setOrientation(state, organizations) {
      state.organizations = organizations
    },
    setClientType(state, clientTypes) {
      state.clientTypes = clientTypes
    },
    setClientTiers(state, clientTiers) {
      state.clientTiers = clientTiers
    },
    setCountriesOrRegions(state, countriesOrRegions) {
      state.countriesOrRegions = countriesOrRegions
    },
    setTimeZones(state, timeZones) {
      state.timeZones = timeZones
    },
    setBusinessAreas(state, businessAreas) {
      state.businessAreas = businessAreas
    },
    setContactTypes(state, contactTypes) {
      state.contactTypes = contactTypes
    },
    setInvoiceFileTypes(state, invoiceFileTypes) {
      state.invoiceFileTypes = invoiceFileTypes
    },
    setIMTypes(state, IMTypes) {
      state.IMTypes = IMTypes
    },
    setCommunicationMethods(state, communicationMethods) {
      state.communicationMethods = communicationMethods
    },
    setCommunicationCategories(state, communicationCategories) {
      state.communicationCategories = communicationCategories
    },
    setSalesBUs(state, salesBUs) {
      state.salesBUs = salesBUs
    },
    setMyBUs(state, myBUs) {
      state.myBUs = myBUs
    },
    setOperateBUs(state, operateBUs) {
      state.operateBUs = operateBUs
    },
    setOwnersBUs(state, ownersBUs) {
      state.ownersBUs = ownersBUs
    },
    setAllHrSalesBUs(state, allHrSalesBUs) {
      state.allHrSalesBUs = allHrSalesBUs
    },
    setCurrencies(state, currencies) {
      state.currencies = currencies
    },
    setClientStatusTypes(state, clientStatusTypes) {
      state.clientStatusTypes = clientStatusTypes
    },
    setClientOperateTypes(state, clientOperateTypes) {
      state.clientOperateTypes = clientOperateTypes
    },
    setRivals(state, rivals) {
      state.rivals = rivals
    },
    setRivalLevels(state, rivalLevels) {
      state.rivalLevels = rivalLevels
    },
    setClientValues(state, clientValues) {
      state.clientValues = clientValues
    },
    setDemandTypes(state, demandTypes) {
      state.demandTypes = demandTypes
    },
    setForecastPOs(state, forecastPOs) {
      state.forecastPOs = forecastPOs
    },
    setInternalLocalizationProcess(state, internalLocalizationProcess) {
      state.internalLocalizationProcess = internalLocalizationProcess
    },
    setProcurementProcess(state, procurementProcess) {
      state.procurementProcess = procurementProcess
    },
    setPainPoints(state, painPoints) {
      state.painPoints = painPoints
    },
    setCommunicationChannels(state, communicationChannels) {
      state.communicationChannels = communicationChannels
    },
    setCommunicationModes(state, communicationModes) {
      state.communicationModes = communicationModes
    },
    setReviewInfos(state, reviewInfos) {
      state.reviewInfos = reviewInfos
    },
    setPlatforms(state, platforms) {
      state.platforms = platforms
    },
    setTranslationPurpose(state, translationPurpose) {
      state.translationPurpose = translationPurpose
    },
    setQuotationStrategies(state, quotationStrategies) {
      state.quotationStrategies = quotationStrategies
    },
    setHrOrg(state, hrOrg) {
      state.hrOrg = hrOrg
    },
    setClientContractTypes(state, clientContractTypes) {
      state.clientContractTypes = clientContractTypes
    },
    setEciCompanies(state, eciCompanies) {
      state.eciCompanies = eciCompanies
    },
    setPaymentMethods(state, paymentMethods) {
      state.paymentMethods = paymentMethods
    },
    setFreezeReasons(state, freezeReasons) {
      state.freezeReasons = freezeReasons
    }
  },
  actions: {
    async getOrganizations({ commit, state }, forceUpdate) {
      if (state.organizations.length === 0 || forceUpdate) {
        commit('setOrientation', await Dict.getOrganization())
      }
    },
    async getClientTypes({ commit, state }) {
      if (state.clientTypes.length === 0) {
        commit('setClientType', await Dict.getClientTypes())
      }
    },
    async getClientTiers({ commit, state }) {
      if (state.clientTiers.length === 0) {
        commit('setClientTiers', await Dict.getClientTiers())
      }
    },
    async getCountriesOrRegions({ commit, state }) {
      if (state.countriesOrRegions.length === 0) {
        commit('setCountriesOrRegions', await Dict.getCountriesOrRegions())
      }
    },
    async getTimeZones({ commit, state }) {
      if (state.timeZones.length === 0) {
        commit('setTimeZones', await Dict.getTimeZones())
      }
    },
    async getBusinessAreas({ commit, state }) {
      if (state.businessAreas.length === 0) {
        commit('setBusinessAreas', await Dict.getBusinessAreas())
      }
    },
    async getInvoiceFileTypes({ commit, state }) {
      if (state.invoiceFileTypes.length === 0) {
        commit('setInvoiceFileTypes', await Dict.getInvoiceFileTypes())
      }
    },
    async getContactTypes({ commit, state }) {
      if (state.contactTypes.length === 0) {
        commit('setContactTypes', await Dict.getContactTypes())
      }
    },
    async getIMTypes({ commit, state }) {
      if (state.IMTypes.length === 0) {
        commit('setIMTypes', await Dict.getIMTypes())
      }
    },
    async getCommunicationMethods({ commit, state }) {
      if (state.communicationMethods.length === 0) {
        commit('setCommunicationMethods', await Dict.getCommunicationMethods())
      }
    },
    async getCommunicationCategories({ commit, state }) {
      if (state.communicationCategories.length === 0) {
        commit('setCommunicationCategories', await Dict.getCommunicationCategories())
      }
    },
    async getSalesBUs({ commit, state }) {
      if (state.salesBUs.length === 0) {
        commit('setSalesBUs', await Dict.getSalesBUs())
      }
    },
    async getMyBUs({ commit, state }) {
      if (state.myBUs.length === 0) {
        commit('setMyBUs', await Dict.getMyBUs())
      }
    },
    async getOperateBUs({ commit, state }) {
      if (state.myBUs.length === 0) {
        commit('setOperateBUs', await Dict.getOperateBUs())
      }
    },
    async getOwnersBUs({ commit, state }, clientId) {
      if (state.ownersBUs.length === 0) {
        commit('setOwnersBUs', await Dict.getOwnersBUs(clientId))
      }
    },
    async getAllHrSalesBUs({ commit, state }) {
      if (state.allHrSalesBUs.length === 0) {
        commit('setAllHrSalesBUs', await Dict.getAllHrSalesBUs())
      }
    },
    async getCurrencies({ commit, state }) {
      if (state.currencies.length === 0) {
        commit('setCurrencies', await Dict.getCurrencies())
      }
    },
    async getClientStatusTypes({ commit, state }) {
      if (state.clientStatusTypes.length === 0) {
        commit('setClientStatusTypes', await Dict.getClientStatusTypes())
      }
    },
    async getClientOperateTypes({ commit, state }) {
      if (state.clientOperateTypes.length === 0) {
        commit('setClientOperateTypes', await Dict.getClientOperateTypes())
      }
    },
    async getRivals({ commit, state }) {
      if (state.rivals.length === 0) {
        commit('setRivals', await Dict.getRivals())
      }
    },
    async getRivalLevels({ commit, state }) {
      if (state.rivalLevels.length === 0) {
        commit('setRivalLevels', await Dict.getRivalLevels())
      }
    },
    async getClientValues({ commit, state }) {
      if (state.clientValues.length === 0) {
        commit('setClientValues', await Dict.getClientValues())
      }
    },
    async getDemandTypes({ commit, state }, payload = { pid: 0, sub: true, force: false }) {
      if (state.demandTypes.length === 0 || payload.force) {
        const allItem = []
        const data = await Dict.getDemandTypes(payload.pid)
        if (Array.isArray(data) && data.length > 0) {
          for (const item of data) {
            allItem.push(item)
            if (payload.sub) {
              const subData = await Dict.getDemandTypes(item.value)
              allItem.push(...subData)
            }
          }
        }
        commit('setDemandTypes', allItem)
      }
    },
    async getForecastPOs({ commit, state }) {
      if (state.forecastPOs.length === 0) {
        commit('setForecastPOs', await Dict.getForecastPOs())
      }
    },
    async getInternalLocalizationProcess({ commit, state }) {
      if (state.internalLocalizationProcess.length === 0) {
        commit('setInternalLocalizationProcess', await Dict.getInternalLocalizationProcess())
      }
    },
    async getProcurementProcess({ commit, state }) {
      if (state.procurementProcess.length === 0) {
        commit('setProcurementProcess', await Dict.getProcurementProcess())
      }
    },
    async getPainPoints({ commit, state }) {
      if (state.painPoints.length === 0) {
        commit('setPainPoints', await Dict.getPainPoints())
      }
    },
    async getCommunicationChannels({ commit, state }) {
      if (state.communicationChannels.length === 0) {
        commit('setCommunicationChannels', await Dict.getCommunicationChannels())
      }
    },
    async getCommunicationModes({ commit, state }) {
      if (state.communicationModes.length === 0) {
        commit('setCommunicationModes', await Dict.getCommunicationModes())
      }
    },
    async getReviewInfos({ commit, state }) {
      if (state.reviewInfos.length === 0) {
        commit('setReviewInfos', await Dict.getReviewInfos())
      }
    },
    async getPlatforms({ commit, state }) {
      if (state.platforms.length === 0) {
        commit('setPlatforms', await Dict.getPlatforms())
      }
    },
    async getTranslationPurpose({ commit, state }) {
      if (state.translationPurpose.length === 0) {
        commit('setTranslationPurpose', await Dict.getTranslationPurpose())
      }
    },
    async getQuotationStrategies({ commit, state }) {
      if (state.quotationStrategies.length === 0) {
        commit('setQuotationStrategies', await Dict.getQuotationStrategies())
      }
    },
    async getHROrg({ commit, state }) {
      if (state.hrOrg.length === 0) {
        commit('setHrOrg', await Dict.getHROrg())
      }
    },
    async getClientContractTypes({ commit, state }) {
      if (state.clientContractTypes.length === 0) {
        commit('setClientContractTypes', await Dict.getClientContractTypes())
      }
    },
    async getEciCompanies({ commit, state }) {
      if (state.eciCompanies.length === 0) {
        commit('setEciCompanies', await Dict.getEciCompanies())
      }
    },
    async getPaymentMethods({ commit, state }) {
      if (state.paymentMethods.length === 0) {
        commit('setPaymentMethods', await Dict.getPaymentMethods())
      }
    },
    async getFreezeReasons({ commit, state }) {
      if (state.freezeReasons.length === 0) {
        commit('setFreezeReasons', await Dict.getFreezeReasons())
      }
    }
  }
}
